package com.ld.admin.controller.oa.work;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.controller.AdminBasicController;
import com.ld.admin.model.oa.work.TodayTaskModel;
import com.ld.admin.service.oa.work.TodayTaskService;
import com.ld.admin.service.oa.work.UserRelationService;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.web.model.SysUser;
import com.ld.shieldsb.common.web.util.Web;
import com.ld.shieldsb.dao.model.QueryModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 今日任务
 * 
 * @ClassName TodayTaskController
 * @author 黄升军
 * @date 2019年08月06日 10:26:33
 *
 */
@Api(tags = "今日任务")
@Controller(value = "")
@RequestMapping("${shieldsb.adminPath}/oa/work/todayTask")
public class TodayTaskController extends AdminBasicController<TodayTaskModel> {
    @Autowired
    protected TodayTaskService todayTaskService;
    @Autowired
    protected UserRelationService userRelationService;

    @ApiOperation(value = "首页", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String index() {
        todayTaskService.permission();
        return "a/oa/work/todaytask/todaytask_header";
    }

    @ApiOperation(value = "到搜索页面", notes = "")
    @RequestMapping(path = "/toSearch", method = RequestMethod.GET)
    public String toSearch(HttpSession session, String sign) {
        session.setAttribute("sign", sign);
        todayTaskService.getUserListBySign(sign);

        return "a/oa/work/todaytask/todaytask_search";
    }

    @ApiOperation(value = "搜索结果", notes = "")
    @RequestMapping(path = "/search", method = RequestMethod.POST)
    public String search(HttpSession session) {
        String sign = (String) session.getAttribute("sign");
        QueryModel queryModel = todayTaskService.getSearchQueryModel(request, TodayTaskModel.class);
        todayTaskService.getTaskBySign(sign, queryModel);
        todayTaskService.getPageBean(request, getModelClass(), queryModel);
        return "a/oa/work/todaytask/todaytask_list";
    }

    @ApiOperation(value = "今日任务展示", notes = "")
    @RequestMapping(path = "/show", method = RequestMethod.GET)
    public String show() {
        long id = getLong("id");
        TodayTaskModel todayTask = todayTaskService.getOne(request, getModelClass(), id);
        SysUser sysUser = todayTaskService.BaseDAO.findById(SysUser.class, todayTask.getUserId());// 根据userid获取用户
        request.setAttribute("sysUser", sysUser);
        return "a/oa/work/todaytask/todaytask_show";
    }

    @ApiOperation(value = "跳转到添加/修改今日任务", notes = "")
    @RequestMapping(path = "/toAddOrUpdate", method = RequestMethod.GET)
    public String toAddOrUpd() {
        long id = getLong("id");
        todayTaskService.getOne(request, getModelClass(), id);
        todayTaskService.getUserList();
        return "a/oa/work/todaytask/todaytask_update";
    }

    @ApiOperation(value = "添加/修改今日任务", notes = "")
    @RequestMapping(path = "/addOrUpdate", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object addOrUpd(@Valid TodayTaskModel model) {
        return todayTaskService.addOrUpdate(model);
    }

    @ApiOperation(value = "删除今日任务", notes = "")
    @RequestMapping(path = "/del", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object del() {
        long id = getLong("id");
        Result result = todayTaskService.remove(getModelClass(), id);
        return Web.Response.result(result);
    }

}